package com.lgkj.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.MovieNumAdapter;
import com.lgkj.bean.MovieDetailsBean;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.SharedPreferencesUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/9 0009.
 */

public class MovieDetailsActivity extends BaseActivity {
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.gv_nun)
    GridView gvNun;
    @BindView(R.id.bt_play)
    Button btPlay;
    private String video_id;
    private MovieDetailsBean bean;
    private MovieNumAdapter adapter;
    String vidoeName = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_movie_details;
    }

    @Override
    public void initView() {
        btPlay.setVisibility(View.GONE);
        setTitle("电影详情");

        video_id = getIntent().getStringExtra("video_id");
        adapter = new MovieNumAdapter(MovieDetailsActivity.this);
        gvNun.setAdapter(adapter);
        gvNun.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                showDilog(adapter.getItem(i));
            }
        });



    }

    private void sendOpen(String url) {
        String ip = SharedPreferencesUtils.getParam(MovieDetailsActivity.this, "ip", "").toString();
        showDiaLog("");
        final String phone = SharedPreferencesUtils.getParam(MovieDetailsActivity.this, "phone", "").toString();
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("open", "item4");
        map.put("id", url);
        map.put("contral", "");
        HttpUtils.getHttp(map, ip + CommonConstant.control, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {

                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }

    private void paly() {
        showDiaLog("正在打开");
        String ip = SharedPreferencesUtils.getParam(MovieDetailsActivity.this, "ip", "").toString();
        final String phone = SharedPreferencesUtils.getParam(MovieDetailsActivity.this, "phone", "").toString();
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("contral", "sure");
        HttpUtils.getHttp(map, ip + CommonConstant.control, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {

                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
                toastLong("当前不是一个wifi，请选链接一个wifi", 0);
            }
        });
    }

    private void showDilog(final String url) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MovieDetailsActivity.this);
        builder.setTitle("请选择播放机型");
        //    指定下拉列表的显示数据
        final String[] cities = {"终端机上播放", "本手机上播放"};

        //    设置一个下拉的列表选择项
        builder.setItems(cities, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    sendOpen(url);
                    btPlay.setVisibility(View.VISIBLE);
                } else {
                    Intent intent = new Intent(MovieDetailsActivity.this, WebViewActivity.class);
                    intent.putExtra("url", url);
                    intent.putExtra("title", vidoeName);
                    startActivity(intent);
                }
            }
        });
        builder.show();
    }


    @Override
    public void initData() {
        showDiaLog("正在获取数据");
        Map<String, String> map = new HashMap<>();
        map.put("id", video_id);
        HttpUtils.postHttp(map, CommonConstant.VideoInfo, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("status") == 1) {
                    bean = new Gson().fromJson(String.valueOf(jsonObject), MovieDetailsBean.class);
                    if (bean != null && bean.getData() != null) {
                        for (int i = 0; i < bean.getData().getVideo_url().size(); i++) {
                            adapter.setUrl(bean.getData().getVideo_url().get(i));
                        }
                        adapter.notifyDataSetChanged();
                        vidoeName = bean.getData().getVideo_name();
                        tvName.setText(vidoeName);
                        tvDesc.setText(bean.getData().getVideo_text());
                        CommonUtils.LoadImage(MovieDetailsActivity.this, ivImg, bean.getData().getVideo_img(), 0);
                    }
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
                toastLong("当前不是一个wifi，请选链接一个wifi", 0);
            }
        });
    }

    @OnClick(R.id.bt_play)
    public void onClick() {
        paly();
    }
}
